/*
 * @Description:表格数据接口
 * @Autor: scy😊
 * @Date: 2021-01-12 11:31:47
 * @LastEditors: scy😊
 * @LastEditTime: 2021-01-23 17:15:26
 */

import { httpcli } from './http'
import { RootObject } from '/@/model/rootObject'
import { ContentType, Method } from 'axios-mapper'
import { ArticleModel } from '/@/model/articleModel'
import { ArticleList } from '/@/model/articleList'
import Assert from '../utils/shf/Assert'

export const defaultArticleModel: ArticleModel = {
  id: 0,
  status: '',
  title: '',
  fullContent: '',
  abstractContent: '',
  sourceURL: '',
  imageURL: '',
  timestamp: '',
  platforms: '',
  disableComment: false,
  importance: '',
  author: '',
  reviewer: '',
  type: '',
  pageviews: '',
  jingbaoqiType: ""//增加此行
}

export enum ArticleType {
  ZBWZ = 'zhanbeiwuzi',
  ZYDW = 'zhuanyeduiwu',
  ZHJG = 'zhihuijigou',
  JJMB = 'jingjimubiao',
  YJSZ = 'yujingshezhi',
  YASZ = 'yuanshezhi',

  MAPPOI = 'MapPoi',

  SSYS = 'Shusanyaosu',

  SBDA = 'Shebeidangan',
  GJRZ = 'Gaojingrizhi',

  TZFW = 'Tiaozhuanfangwen',
}

export const getArticles = (params: any, type: string = ArticleType.ZBWZ) => {
  let query = params;
  if (query.sort !== undefined) delete query.sort;
  return httpcli().request<RootObject<ArticleList<ArticleModel>>>(`${type}/getArticles`, Method.POST, query, ContentType.form)
}

export const getArticle = (params: any, type: string = ArticleType.ZBWZ) => {
  return httpcli().request<RootObject<ArticleModel>>(`${type}/getArticleInfo`, Method.GET, params, ContentType.form)
}

export const createArticle = async (data: any, type: string = ArticleType.ZBWZ, isCheck: boolean = true) => {
  console.log('createArticel isCheck = ', isCheck);

  let query = { ...data, check: isCheck };
  if (query.id !== undefined) {
    delete query.id;
  }
  let res: any = await httpcli().request<RootObject<ArticleModel>>(`${type}/createArticle`, Method.POST, query, ContentType.form);
  if (res?.error) {
    Assert.isFalse(true, res.error + "");
    return Promise.resolve();
  }
  return Promise.resolve(res.id);
}

export const updateArticle = async (params: any, type: string = ArticleType.ZBWZ) => {
  let res: any = await httpcli().request<RootObject<ArticleModel>>(`${type}/updateArticle`, Method.POST, params, ContentType.form);
  if (res?.error) {
    Assert.isFalse(true, res.error + "");
    return Promise.resolve();
  }
  return Promise.resolve(null);
}

export const updateArticleByTitle = (params: any, type: string = ArticleType.ZBWZ) => {
  return httpcli().request<RootObject<ArticleModel>>(`${type}/updateArticleByTitle`, Method.POST, params, ContentType.form)
}


export const deleteArticle = (id: number, type: string = ArticleType.ZBWZ) => {
  let query = {
    id: id
  };

  return httpcli().request<RootObject<ArticleModel>>(`${type}/deleteArticle/`, Method.POST, query, ContentType.form)
}

export const deleteArticleByName = (name: any, type: string = ArticleType.ZBWZ) => {
  let query = {
    title: name
  };

  return httpcli().request<RootObject<ArticleModel>>(`${type}/deleteArticleByTitle/`, Method.POST, query, ContentType.form)
}

export const deleteArticleByImportanceAndTitle = (tags: any, name: any, type: string = ArticleType.ZBWZ) => {
  let query = {
    importance: tags,
    title: name,
  };

  return httpcli().request<RootObject<ArticleModel>>(`${type}/deleteArticleByImportanceAndTitle/`, Method.POST, query, ContentType.form)
}



export interface Pageviews {
  pageviews: any
}

export const getPageviews = (params: any) => {
  return httpcli().request<RootObject<Pageviews>>('pageviews', Method.GET, params, ContentType.form/*ContentType.json*/)
}

